﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace RFPL
{
    public partial class ClubList : Form
    {
        
        public ClubList()
        {
            InitializeComponent();
        }

        private void ClubList_Load(object sender, EventArgs e)
        {
            UpdateClubList();
        }

        private void UpdateClubList() {

            dgvClubs.Rows.Clear();

            SqlDataReader dataReader = Global.getDataReader("[getAllClubsPreview]");
            while (dataReader.Read())
            {
                dgvClubs.Rows.Add(dataReader["id"], dataReader["label"], dataReader["name"]);

            }
            dataReader.Close();
            Global.closeConnection();       
        }

        private void add_Click(object sender, EventArgs e)
        {
            ClubInfo ci = new ClubInfo(0);
            ci.ShowDialog();

            UpdateClubList();
        }

        private void edit_Click(object sender, EventArgs e)
        {
            int clubId = Convert.ToInt32(dgvClubs.SelectedRows[0].Cells["id"].Value);
            ClubInfo ci = new ClubInfo(clubId);
            ci.ShowDialog();

            UpdateClubList();
        }

        private void dgvClubs_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvClubs.SelectedRows.Count > 0)
            {
                edit.Enabled = true;
                del.Enabled = true;
            }
            else {
                edit.Enabled = false;
                del.Enabled = false;
            }
        }

        private void del_Click(object sender, EventArgs e)
        {
            int clubId = Convert.ToInt32(dgvClubs.SelectedRows[0].Cells["id"].Value);

            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = Global.connectionString;
            conn.Open();
            SqlCommand myCommand = conn.CreateCommand();
            myCommand.CommandType = CommandType.StoredProcedure;
            myCommand.CommandText = "[delClub]";
            myCommand.Parameters.Add("@id", SqlDbType.Int).Value = clubId;
            myCommand.ExecuteReader();
            conn.Close();

            UpdateClubList();
        }
    }
}
